/**
 * 物品
 */
const Router = require('@koa/router');
const { Item } = require('../db/schemas')

const { Language } = require('../env');
const { genResponse, raw$project } = require('../utils');

const router = new Router({ prefix: '/item' })

router.get('/', async (ctx) => {
  const results = await Item.aggregate([
    {
      $project: {
        _id: 0,
        id: 1,
        name: `$preview_item.name.${Language}`, // 名称
        // quality: `$preview_item.quality.name.${Language}`, // 品质
        // item_class: {
        //   id: `$item_class.id`,
        //   name: `$item_class.name.${Language}`,
        // },
        // item_subclass: {
        //   id: `$item_subclass.id`,
        //   name: `$item_subclass.name.${Language}`,
        // },
        inventory_type: `$preview_item.inventory_type.name.${Language}`,
        binding: `$preview_item.binding.name.${Language}`,
        armor: `$preview_item.armor.display.display_string.${Language}`,
        stats: {
          $map: {
            input: '$preview_item.stats',
            as: 'stat',
            in: `$$stat.display.display_string.${Language}`
          }
        },
        sell_price: {
          $concat: [
            `$preview_item.sell_price.display_strings.header.${Language}`,
            `$preview_item.sell_price.display_strings.silver.${Language}`,
            '银',
            `$preview_item.sell_price.display_strings.copper.${Language}`,
            '铜'
          ]
        },
        level: `$preview_item.level.display_string.${Language}`,
        durability: `$preview_item.durability.display_string.${Language}`,
        updated_at: 1,
        raw: '$$ROOT'
      }
    },
    raw$project,
  ])
  ctx.body = genResponse(true, results)
})

module.exports = router